package org.springblade.modules.repair.controller;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.IntegerPool;
import org.springblade.modules.repair.entity.MaintainMalfunctionWork;
import org.springblade.modules.repair.service.IMaintainMalfunctionWorkService;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.vo.FaultFeedbackVO;
import org.springblade.modules.repair.vo.MalFunctionDetailVO;
import org.springblade.modules.repair.vo.MyApproveDetailVO;
import org.springblade.modules.repair.vo.MyApproveVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的申请接口
 *
 * @author Sharry
 * @since 2023/4/6
 */
@RestController
@Slf4j
@RequestMapping("/repair/my/approve")
@AllArgsConstructor
@Api(tags = "我的申请", value = "/repair/my/approve")
public class MyApproveController {

	private final IMaintainPlanContentService maintainPlanContentService;

	private final IMaintainMalfunctionWorkService maintainMalfunctionWorkService;

	/**
	 * 查询维修报障列表，根据审核状态区分 ： null-全部， 1-待审批，2-通过，3-驳回， 4-撤回
	 */
	@ApiOperation(value = "我的申请-维修报障列表")
	@ApiOperationSupport(order = 10)
	@PostMapping("/list")
	public R<IPage<MyApproveVO>> getMyApproveList(@RequestBody Map<String, Integer> status, @RequestBody Query query) {
		Integer approveStatus = status.get("approveStatus");
		Long userId = AuthUtil.getUserId();
		if (Func.isNull(userId)) {
			throw new ServiceException("请先登录，please login first");
		}
		log.info("即将根据当前用户查询报障申请列表，当前用户id：{}", userId);
		IPage<MyApproveVO> myApproveVOList = maintainPlanContentService.getMyApproveList(userId, approveStatus, Condition.getPage(query));
		return R.data(myApproveVOList);
	}

	/**
	 * 报障申请详情
	 */
	@ApiOperation("报障申请详情")
	@ApiOperationSupport(order = 20)
	@PostMapping("/detail")
	public R<MyApproveDetailVO> getMyApproveDetail(@RequestBody Map<String, Long> planId) {
		Long id = planId.get("planId");
		// 直接复用maintainMalfunctionWorkService.malfunctionDetail(planId)
		MalFunctionDetailVO malFunctionDetailVO = maintainMalfunctionWorkService.malfunctionDetail(id);
		MyApproveDetailVO myApproveDetailVO = new MyApproveDetailVO();
		// 关联id列表
		List<Long> relatedWorkOrderIds = getFromPlanIdByOwnId(id);
		// 是否允许修改，没有关联时允许
		Boolean isAllowModify = Func.isEmpty(relatedWorkOrderIds);

		BeanUtils.copyProperties(malFunctionDetailVO, myApproveDetailVO);
		myApproveDetailVO.setPlanDate(malFunctionDetailVO.getPlanDate());
		myApproveDetailVO.setPlanFinishDate(malFunctionDetailVO.getPlanFinishDate());
		myApproveDetailVO.setSourcePlanType(malFunctionDetailVO.getSourcePlanType());
		myApproveDetailVO.setSourcePlanTypeName(malFunctionDetailVO.getSourcePlanTypeName());
		myApproveDetailVO.setRelatedWorkOrderIds(relatedWorkOrderIds);
		myApproveDetailVO.setIsAllowModify(isAllowModify);
		// 查询预计维修时间
		String preTime = maintainMalfunctionWorkService.getPreTime(id);
		myApproveDetailVO.setPreTime(preTime);

		// 补充原方法缺失字段
		FaultFeedbackVO faultFeedbackVO = myApproveDetailVO.getFaultFeedbackVO();
		FaultFeedbackVO feedbackVO = maintainMalfunctionWorkService.getFeedbackMessage(id);
		if (Func.isNotEmpty(faultFeedbackVO) && Func.isNotEmpty(feedbackVO) && feedbackVO.getRemark() == null) {
			feedbackVO.setRemark("");
		}
		Integer approveStatus = maintainMalfunctionWorkService.getApproveStatus(id);
		if (approveStatus == null) {
			myApproveDetailVO.setApproveStatus(malFunctionDetailVO.getStatus());
		} else {
			myApproveDetailVO.setApproveStatus(approveStatus);
		}

		myApproveDetailVO.setFaultFeedbackVO(feedbackVO);
		return R.data(myApproveDetailVO);
	}

	private List<Long> getFromPlanIdByOwnId(Long planId) {
		return maintainPlanContentService.getFromPlanIdByOwnId(planId);
	}


}
